package com.sky.service.impl;

import com.sky.service.ReportService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private WorkspaceService workspaceService;

    /**
     * 报表导出  统计最近30天的数据，并导出报表
     *
     * @param response 响应HttpServletResponse
     */
    @Override
    public void businessDataExport(HttpServletResponse response) {
        try (
                // 1.读取Excel文件
                InputStream is = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");

                ServletOutputStream os = response.getOutputStream();
        ) {
            // 2.查询概览数据（最近30天）
            LocalDate begin = LocalDate.now().minusDays(30);
            LocalDate end = LocalDate.now().minusDays(1);
            BusinessDataVO data = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));

            // 3.填充数据
            XSSFWorkbook excel = new XSSFWorkbook(is);
            XSSFSheet sheet = excel.getSheetAt(0);
            // 3.1 日期说明
            sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);
            // 3.2 概览数据
            // 营业额
            sheet.getRow(3).getCell(2).setCellValue(data.getTurnover());
            // 订单完成率
            sheet.getRow(3).getCell(4).setCellValue(data.getOrderCompletionRate());
            // 新增用户数
            sheet.getRow(3).getCell(6).setCellValue(data.getNewUsers());
            // 有效订单
            sheet.getRow(3).getCell(2).setCellValue(data.getValidOrderCount());
            // 平均客单价
            sheet.getRow(4).getCell(4).setCellValue(data.getUnitPrice());

            // 4.查询每一天的数据（最近30天）：明细数据查询
            for (int i = 0; i < 30; i++) {
                LocalDate date = begin.plusDays(i);
                data = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
                XSSFRow row = sheet.getRow(7 + i);
                row.getCell(1).setCellValue(date.toString());
                row.getCell(2).setCellValue(data.getTurnover());
                row.getCell(3).setCellValue(data.getValidOrderCount());
                row.getCell(4).setCellValue(data.getOrderCompletionRate());
                row.getCell(5).setCellValue(data.getUnitPrice());
                row.getCell(6).setCellValue(data.getNewUsers());
            }
            excel.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
